package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.AdminInfo;
import com.example.computer_exam_training_sys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    public AdminService adminService;

    @GetMapping("/list")
    public List<AdminInfo> getAdminList() {
        return adminService.getAdminList();
    }

    @GetMapping("/find")
    public AdminInfo getAdminById(@RequestParam Integer admin_id) {
        return adminService.getAdminById(admin_id);
    }

    @GetMapping("/search")
    public List<AdminInfo> searchAdmins(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) Integer status) {
        return adminService.searchAdmins(name, role, status);
    }

    @PutMapping("/update/{admin_id}")
    public Map<String, Object> updateAdmin(
            @PathVariable("admin_id") Integer admin_id,
            @RequestBody AdminInfo adminInfo
    ) {
        Map<String, Object> response = new HashMap<>();
        try {
            adminInfo.setAdminId(admin_id);
            boolean result = adminService.updateAdmin(adminInfo);
            if (result) {
                response.put("status", "success");
                response.put("message", "管理员信息更新成功");
            } else {
                response.put("status", "error");
                response.put("message", "管理员信息更新失败");
            }
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "发生异常: " + e.getMessage());
        }
        return response;
    }
}